package com.nstar.carousel.base
{
	import com.nstar.base.event.AdvanceEvent;
	import com.nstar.carousel.components.*;
	import com.nstar.carousel.model.ProductVO;
	import com.nstar.carousel.utils.CarouselEffectUtils;
	
	import flash.events.MouseEvent;
	
	import mx.containers.Canvas;
	import mx.controls.Text;
	import mx.events.FlexEvent;
	
	/**
	 * 基类名称：轮播组件基类
	 * 基类说明：提供轮播组件的基本属性、方法和事件支持
	 * 开发时间：2010.02.25
	 */ 
	public class CarouselBaseClass extends Canvas
	{
		
		private var _dataProvider:Array;//数据源
		private var _layout:String;//布局方式
		private var _selectedItem:ProductVO = null;//当前选中的记录
		private var _selectedIndex:int = -1;//当前选中记录的索引
		
		public var imageStage:ImageStageBaseClass;//图片展示舞台
		public var txtTitle:Text;//当前图片的标题
		public var txtDesc:Text;//当前图片的描述信息
		public var indexIndicator:IndexIndicator;//索引指示器
		public var navigator:NavigatorControl;//导航控制器
		[Bindable]public var imageStageWidth:Number = 800;//图片显示宽度
		[Bindable]public var imageStageHeight:Number = 350;//图片显示高度
		
		public static const PRIOR:String = "prior";
		public static const NEXT:String = "next";
				
		public function CarouselBaseClass()
		{
			super();
			this.addEventListener(FlexEvent.CREATION_COMPLETE,init);
		}
		
		private function init(event:FlexEvent):void{
			this.addEventListener(MouseEvent.MOUSE_WHEEL,mouseWheelHandler);
			navigator.addEventListener(NavigatorControl.SELECTED,navigatorSelectedHandler);
			validate();
			
			initEffect();
		}
		
		private function navigatorSelectedHandler(event:AdvanceEvent):void{
			this.selectedIndex = int(event.request);
		}
		
		private function validate():void{
			if(!imageStage || !txtTitle || !indexIndicator){
				throw new Error("图片显示组件、标题和索引指示器必须存在！");
			}
		}
		
		public function initEffect():void{
			imageStage.effect = CarouselEffectUtils.createEffect(CarouselEffectUtils.EFFECT_LIST);
		}		
		
		
		//鼠标滚轮支持
		private function mouseWheelHandler(event:MouseEvent):void{
			if(event.delta<0){
				next();
			}else{
				prior();
			}
		}		
		
		//下一个
		public function next(direction:String="left"):ProductVO{
			imageStage.direction = direction;
			var idx:int = selectedIndex;
			idx++;
			if(idx>_dataProvider.length-1){
				idx = _dataProvider.length -1;
			}
			selectedIndex = idx;
			selectedItem = dataProvider[selectedIndex];
			if(txtTitle){
				txtTitle.text = selectedItem.title;
			}
			return selectedItem;
		}
		
		//上一个
		public function prior(direction:String="right"):ProductVO{
			imageStage.direction = direction;
			var idx:int = selectedIndex;
			idx--;
			if(idx<0){
				idx = 0;
			}
			selectedIndex = idx;
			selectedItem = dataProvider[selectedIndex];
			if(txtTitle){
				txtTitle.text = selectedItem.title;
			}
			
			return selectedItem;
		}
		
		public function set dataProvider(value:Array):void{
			validate();
			
			this._dataProvider = value;
			//设置默认值
			if(_dataProvider && _dataProvider.length>0){
				imageStage.dataProvider = _dataProvider;
				navigator.dataProvider = _dataProvider;
				
				this.selectedItem = _dataProvider[0];
				selectedIndex = 0;
				txtTitle.text = selectedItem.title;
			}
		}
		
		public function get dataProvider():Array{
			return this._dataProvider;
		}
		
		
		public function set layout(value:String):void{
			this._layout = value;
//			switch(_layout){
//				case LAYOUT_STANDARD:
//					break;
//				case LAYOUT_FLOW:
//					break;
//				case LAYOUT_WALL:
//					break;
//			}
		}
		
		public function get layout():String{
			return this._layout;
		}
		
		public function set selectedItem(value:ProductVO):void{
			this._selectedItem = value;
		}
		
		public function get selectedItem():ProductVO{
			return this._selectedItem;
		}
		
		public function set selectedIndex(value:int):void{
			this._selectedIndex = value;
			
			imageStage.selectedIndex = this._selectedIndex;
			navigator.selectedIndex = this._selectedIndex;
			indexIndicator.init(imageStage.selectedIndex+1,this._dataProvider.length);
		}
		
		public function get selectedIndex():int{
			return this._selectedIndex;
		}		
	}
}